/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.examples.fraclite.orb.helloworld;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.factory.GenericFactory;
import org.objectweb.fractal.api.type.ComponentType;
import org.objectweb.fractal.api.type.InterfaceType;
import org.objectweb.fractal.api.type.TypeFactory;
import org.objectweb.fractal.util.Fractal;

import fr.jade.fraclite.orb.common.api.NamingService;
import fr.jade.fraclite.orb.common.ns.Registry;
import fr.jade.fraclite.util.FracLite;

public class ClientLauncher {

  public static void main (final String[] args) throws Exception {

    NamingService ns = Registry.getRegistry();
    
    Component rComp;

    Component boot = ns.lookup("server-host");
    TypeFactory tf = Fractal.getTypeFactory(boot);
    // type of root component
    ComponentType rType = tf.createFcType(new InterfaceType[] {
      tf.createFcItfType("m", "fr.jade.examples.fraclite.orb.helloworld.Main", false, false, false)
    });
    // type of client component
    ComponentType cType = tf.createFcType(new InterfaceType[] {
      tf.createFcItfType("m", "fr.jade.examples.fraclite.orb.helloworld.Main", false, false, false),
      tf.createFcItfType("s", "fr.jade.examples.fraclite.orb.helloworld.Service", true, false, false)
    });
    // type of server component
    ComponentType sType = tf.createFcType(new InterfaceType[] {
      tf.createFcItfType("s", "fr.jade.examples.fraclite.orb.helloworld.Service", false, false, false),
    });

    GenericFactory cf = Fractal.getGenericFactory(boot);

    // create root component
    rComp = cf.newFcInstance(rType, "composite", null);
    // create client component
    Component cComp = cf.newFcInstance(cType, "primitive", "fr.jade.examples.fraclite.orb.helloworld.ClientImpl");
    // create server component
    Component sComp = cf.newFcInstance(sType, "primitive", "fr.jade.examples.fraclite.orb.helloworld.ServerImpl");
    FracLite.getAttributeController(sComp).setAttribute("header", "> ");
    FracLite.getAttributeController(sComp).setAttribute("count", "2");


      // component assembly
    Fractal.getContentController(rComp).addFcSubComponent(cComp);
    Fractal.getContentController(rComp).addFcSubComponent(sComp);
    Fractal.getBindingController(rComp).bindFc("m", cComp.getFcInterface("m"));
    Fractal.getBindingController(cComp).bindFc("s", sComp.getFcInterface("s"));
 
    // start root component
    Fractal.getLifeCycleController(rComp).startFc();

    // call main method
    ((Main)rComp.getFcInterface("m")).main(null);
  }

}
